package com.vincent.kdp.ui.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.vincent.kdp.R;
import com.vincent.kdp.ui.bean.entity.HotCommodity;
import com.vincent.kdp.ui.bean.response.ShopCartListOut;
import com.vincent.kdp.ui.fragment.contract.ShopCartContract;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2017/5/22 0022.
 */

public class ShopCartListAdapter extends BaseAdapter {
    private ViewHolder holder;
    private List<ShopCartListOut.ShopCartBean> mShopCartyList;
    private Context context;

    private OnShoppingCartChangeListener mChangeListener;


    public Boolean isCheckShop=false;//单个选中判断
    public Boolean isAllSelect=false; //全选中状态
    public static Boolean LinCheck=false;// 全选
    public static Map<String,Boolean> map=new HashMap<String,Boolean>();

    private static  HashMap<Integer,Boolean> isSelected=new HashMap<Integer, Boolean>();;


    public ShopCartListAdapter(Context context,List<ShopCartListOut.ShopCartBean> mListGoods) {
        this.context = context;
        this.mShopCartyList = mListGoods;
        initDate();
    }
    private void initDate(){
        for(int i=0; i<mShopCartyList.size();i++) {
            isSelected.put(i,false);
        }
    }

    @Override
    public int getCount() {
        return mShopCartyList.size();
    }

    @Override
    public Object getItem(int position) {
        return mShopCartyList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_lv_shopcart, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        Glide.with(context).load(R.mipmap
                .zikjlogo).placeholder(R.mipmap
                .zikjlogo).into(holder.mCommodityImg);


        holder.mshopcartCheck.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (b) {
                    mShopCartyList.get(position).setStatus("1");;
                    // 将CheckBox的选中状况记录下来
                   // isSelected.put(position, true);
                    isAllSelect =isSelectAllGroup(mShopCartyList);
                    selectAll();
                    notifyDataSetChanged();
                }else {

                    mShopCartyList.get(position).setStatus("0");

                    // 将CheckBox的选中状况记录下来
                    //isSelected.put(position, false);
                    selectAll();
                    isAllSelect =isSelectAllGroup(mShopCartyList);
                    selectAll();
                    notifyDataSetChanged();
                }
            }
        });

                System.out.println(mShopCartyList.get(position).getStatus());
        if(Integer.parseInt(mShopCartyList.get(position).getStatus())==0){
            holder.mshopcartCheck.setChecked(false);
        }else {
            holder.mshopcartCheck.setChecked(true);
        }
       //holder.mshopcartCheck.setChecked(isSelected.get(position));

        holder.mCommodityNameTv.setText(mShopCartyList.get(position).getGoodsName());

        holder.mPriceTv.setText("￥" + mShopCartyList.get(position).getPrice());
        holder.mCompanyAndHandInch.setText("重量："+mShopCartyList.get(position).getTotalWeight());
        holder.mCompanyAndHandInch2.setText("手寸："+mShopCartyList.get(position).getHandInch()+"号");

//


        return convertView;
    }
    //全选
    private void selectAll() {
        if (mChangeListener != null) {
            mChangeListener.onSelectItem(isAllSelect);
        }
    }
    //配套监听  setOnClicklinsten1
    public void setOnShoppingCartChangeListener(OnShoppingCartChangeListener changeListener) {
        this.mChangeListener = changeListener;
    }



    //配套监听  setOnClicklinsten2
    public interface OnShoppingCartChangeListener {
        void onDataChange(String selectMoney, String selectCount);
        void onSelectItem(boolean isSelectedAll);
    }


    //传递CHECK LISTENER
    public CompoundButton.OnCheckedChangeListener getCheckAdapterListener(){
        return checkListener;
    }
    CompoundButton.OnCheckedChangeListener checkListener=new CompoundButton.OnCheckedChangeListener(){

        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
            switch (compoundButton.getId()){
                case R.id.shopcart_cb:
                    if (b) {
                       isAllSelect = selectAllItem(mShopCartyList, b, (CheckBox) compoundButton);
                       // setSettleInfo();
                        notifyDataSetChanged();
                    }else {

                        isAllSelect = selectAllItem(mShopCartyList, b, (CheckBox) compoundButton);
                      //  setSettleInfo();
                        notifyDataSetChanged();
                    }
                    break;
            }
        }
    };
//    //buy  和删除
//    public View.OnClickListener getClickListener() {
//        return listener;
//    }
//
//    View.OnClickListener listener = new View.OnClickListener(){
//
//        @Override
//        public void onClick(View view) {
//            switch (view.getId()) {
//                //删除
//                case R.id.shopcart_delete:
////                    if(allnum>0){
////                        Orders orders=new Orders();
////                        orders.setList(mListGoods);
////                        orders.setLastact(0);
////                        Intent intent=new Intent(mContext, OrderDetailActivity.class);
////                        intent.putExtra("order",orders);
////                        mContext.startActivity(intent);
////                    }else{
////                        Toast.makeText(mContext, "请勾选产品后提交订单", Toast.LENGTH_SHORT).show();
////                    }
//                //买单
//                case R.id.shopcart_buy:
////                    if(allnum>0){
////                        Orders orders=new Orders();
////                        orders.setList(mListGoods);
////                        orders.setLastact(0);
////                        Intent intent=new Intent(mContext, OrderDetailActivity.class);
////                        intent.putExtra("order",orders);
////                        mContext.startActivity(intent);
////                    }else{
////                        Toast.makeText(mContext, "请勾选产品后提交订单", Toast.LENGTH_SHORT).show();
////                    }
//
//
//                    break;
//            }
//        }
//    };



    /**
     * 全选/全不选  外界Check
     */
    public static boolean selectAllItem(List<ShopCartListOut.ShopCartBean> list, boolean isSelectAll, CheckBox ivCheck) {
        ivCheck.setChecked(isSelectAll);
        if(isSelectAll){
            for (int i = 0; i < list.size(); i++) {
                list.get(i).setStatus("1");
            }
        }else {
            for (int i = 0; i < list.size(); i++) {
                list.get(i).setStatus("0");
            }
        }
        return isSelectAll;
    }
    /**
     * LIST  CHECK 全选数量监听
     */
    private boolean isSelectAllGroup(List<ShopCartListOut.ShopCartBean> list) {
        for (int i = 0; i < list.size(); i++) {
            int is = Integer.parseInt(list.get(i).getStatus());
            if (is==0) {
                return false;
            }
        }
        return true;
    }



    static class ViewHolder {

        @Bind(R.id.shopcart_check_lin)
        LinearLayout mshopcartCheckLin;
        @Bind(R.id.shopcart_check)
        CheckBox mshopcartCheck;
        @Bind(R.id.shopcart_img)
        ImageView mCommodityImg;
        @Bind(R.id.shopcart_name_tv)
        TextView mCommodityNameTv;
        @Bind(R.id.shopcart_price_tv)
        TextView mPriceTv;
        @Bind(R.id.shopcart_style_num_and_handInch_tv)
        TextView mCompanyAndHandInch;
        @Bind(R.id.shopcart_style_num_and_handInch_tv2)
        TextView mCompanyAndHandInch2;
        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
